#include <string>
#include <iostream>
using namespace std;

class Node {
public:
    int id;
    string data;
    Node *next;
    Node(int id, string data) :id(id),data(data),next(nullptr){}
};

class LinkedList {
private:
    Node *head;
public:
    LinkedList() :head(nullptr){}
    // 添加结点数据
    void add(Node *node) {
        if (head == nullptr) {
            head = node;
            return;
        }
        Node *tmpNode = head;
        while (true) {
            if (tmpNode->next == nullptr)
                break;
            tmpNode = tmpNode->next;
        }
        tmpNode->next = node;
    }
    // 遍历结点数据
    void traverse() {
        if (head == nullptr) {
            cout << "An empty list" << endl;
            return;
        }
        Node *tmpNode = head;
        while(true) {
            cout << "id = " << tmpNode->id << ",data = " << tmpNode->data << "\t";
            if (tmpNode->next == nullptr)
                break;
            tmpNode = tmpNode->next;
        }
        cout << endl;
    }
    // 根据id查找
    Node *find_by_id(int id) {
        if (head == nullptr) {
            throw "An empty list";
        }
        Node *tmpNode = head;
        while (true) {
            if (tmpNode->id == id) {
                break;
            }
            if (tmpNode->next == nullptr) {
                tmpNode ==nullptr;
                break;
            }
            tmpNode = tmpNode->next;
        }
        return tmpNode;
    }
};

class HashTable {
private:
    LinkedList *array;
    int size;
    
    // 散列函数
    int hash_func(int id) {
        return id % size;
    }

public:
    HashTable(int size) :size(size) {
        array = new LinkedList[size];
        // for (int i = 0; i < size; i++) {
        //     array[i] = new LinkedList();
        // }
    }
    // 添加
    void add(Node *node) {
        int list_id = hash_func(node->id);
        array[list_id].add(node);
    }
    // 遍历
    void traverse() {
        for (int i = 0; i < size; i++) {
            array[i].traverse();
        }
    }
    // 根据id,查找雇员
    void find_by_id(int id) {
        int list_id = hash_func(id);
        Node *node = array[list_id].find_by_id(id);
        if (node != nullptr) {
            cout << "id = " << id << "data = " << node->data << endl;
        } else {
            cout << "no found" << endl;
        }
    }
};